import logging
from logging.handlers import TimedRotatingFileHandler
import colorlog


log_colors_config = {
    'DEBUG': 'white',  # cyan white
    'INFO': 'green',
    'WARNING': 'yellow',
    'ERROR': 'red',
    'CRITICAL': 'bold_red',
}

logger = logging.getLogger(__name__)

# 输出到控制台
console_handler = logging.StreamHandler()



# 输出到文件
#file_handler = logging.FileHandler(filename='test.log', mode='a', encoding='utf8')
# window日志路径 C:/Users/李呐纳/PycharmProjects/Face_Detection_Recognition-master/faceRecognition/utils/logs/info.log

fch = TimedRotatingFileHandler("/mylogs/face_server/info.log", when='D', encoding="utf-8")
fchError = TimedRotatingFileHandler("/mylogs/face_server/error.log", when='D', encoding="utf-8")


# 日志级别，logger 和 handler以最高级别为准，不同handler之间可以不一样，不相互影响
logger.setLevel(logging.DEBUG)
console_handler.setLevel(logging.DEBUG)
#file_handler.setLevel(logging.INFO)
fchError.setLevel(logging.ERROR)
fch.setLevel(logging.INFO)

# 日志输出格式
file_formatter = logging.Formatter(
    fmt='[%(asctime)s.%(msecs)03d] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
    datefmt='%Y-%m-%d  %H:%M:%S'
)
console_formatter = colorlog.ColoredFormatter(
    fmt='%(log_color)s[%(asctime)s.%(msecs)03d] %(filename)s -> %(funcName)s line:%(lineno)d [%(levelname)s] : %(message)s',
    datefmt='%Y-%m-%d  %H:%M:%S',
    log_colors=log_colors_config
)
console_handler.setFormatter(console_formatter)
#file_handler.setFormatter(file_formatter)
fch.setFormatter(file_formatter)
fchError.setFormatter(file_formatter)

# 重复日志问题：
# 1、防止多次addHandler；
# 2、loggername 保证每次添加的时候不一样；
# 3、显示完log之后调用removeHandler
if not logger.handlers:
    logger.addHandler(console_handler)
    #logger.addHandler(file_handler)
    logger.addHandler(fch)
    logger.addHandler(fchError)
console_handler.close()
#file_handler.close()
fch.close()
fchError.close()

def info(msg):
    logger.info(msg)



